#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(TEST_TARGET_cant)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/cant.h>
#include <varch/cQueue.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "cant.h"
#include "cQueue.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

/////////////////////////////////
//// Simulating CAN BUS     /////
/////////////////////////////////

typedef int (*canbus_receive_t)(uint32_t canid, uint8_t *data, uint16_t length);

typedef struct 
{
    uint8_t devid;
    canbus_receive_t receive;
} CanDevType;

typedef struct 
{
    uint8_t devid;
    uint16_t length;
    uint32_t canid; 
    uint8_t data[64]; 
} CanTpType;

typedef struct 
{
    CanDevType devs[8];
    uint8_t devn;
    cQueue(CanTpType, 1024) tp;
} CanBusType;

static CanBusType canbus;
static uint8_t canbus_maxdev = (uint8_t)(sizeof(canbus.devs) / sizeof(canbus.devs[0]));

static int canbus_attach_dev(uint8_t devid, canbus_receive_t receive)
{
    if (devid >= canbus_maxdev) return -1;
    if (!receive) return -2;

    canbus.devs[devid].devid = devid;
    canbus.devs[devid].receive = receive;

    return 0;
}

static int canbus_transer(uint8_t devid, uint32_t canid, uint8_t *data, uint16_t length)
{
    CanTpType tp;
    int ret = 0;

    if (devid >= canbus_maxdev) return -1;
    if (canbus.devs[devid].devid == 0xFF) return -3;
    if (!data) return -4;
    if (length > 64) return -5;

    tp.devid = devid;
    tp.length = length;
    tp.canid = canid;
    for (uint16_t i = 0; i < length; i++) tp.data[i] = data[i];

    ret = cQueue_push(canbus.tp, tp);
    if (!ret) return -6;

    return 0;
}

static void canbus_init()
{
    memset(&canbus, 0, sizeof(canbus));

    for (uint8_t devid = 0; devid < canbus_maxdev; devid++)
    {
        canbus.devs[devid].devid = 0xFF;
    }

    cQueue_init(canbus.tp);
}

static void canbus_task()
{
    CanTpType tp;

    while (cQueue_pop(canbus.tp, tp)) 
    {
        for (uint8_t devid = 0; devid < canbus_maxdev; devid++)
        {
            if (canbus.devs[devid].devid == 0xFF) continue;
            if (canbus.devs[devid].devid == tp.devid) continue;

            if (canbus.devs[devid].receive) 
            {
                canbus.devs[devid].receive(tp.canid, tp.data, tp.length);
            }
        }
    } 
}

/////////////////////////////////
//// Simulating CAN DEV     /////
/////////////////////////////////

static CANT cant0;
static CANT cant1;

static int dev0_can_transmit(uint32_t canid, uint8_t *data, uint16_t length) 
{ 
    int ret = canbus_transer(0, canid, data, length);
    return ret;
}

static int dev1_can_transmit(uint32_t canid, uint8_t *data, uint16_t length)
{
    int ret = canbus_transer(1, canid, data, length);
    return ret;
}

static int dev0_can_receive(uint32_t canid, uint8_t *data, uint16_t length)
{
#if 0
    printf("[DEV0][%08x] ", canid);
    for (int i = 0; i < length; i++)
    {
        printf("%02x ", data[i]);
    }
    printf("\n");
#endif 
    cant_receive(&cant0, canid, data, length);
    return 0;
}

static int dev1_can_receive(uint32_t canid, uint8_t *data, uint16_t length)
{
#if 0
    printf("[DEV1][%08x] ", canid);
    for (int i = 0; i < length; i++)
    {
        printf("%02x ", data[i]);
    }
    printf("\n");
#endif 
    cant_receive(&cant1, canid, data, length);
    return 0;
}

/////////////////////////////////
/////   CANT           //////////
/////////////////////////////////

static CANT cant0 = {
    .config = {
        .channel = 0,
        .baundrate = CANT_BAUDRATE_500K,
        .period = 5,
        .transmit = dev0_can_transmit,
        .receive = NULL,
    },
};

static CANT cant1 = {
    .config = {
        .channel = 0,
        .baundrate = CANT_BAUDRATE_500K,
        .period = 5,
        .transmit = dev1_can_transmit,
        .receive = NULL,
    },
};

static void dev0_cant_task(void)
{
    static uint32_t count = 0;

    count += 5;
    if (count >= 25200000) count = 0;

    if (count % 5 == 0)
    {
        cant_task(&cant0);
    }

    if (count % 1000 == 0)
    {
        static uint8_t data[64];
        static uint32_t sc = 0;
        *(uint32_t *)data = sc++;
        cant_transmit(&cant0, 0x75, data, 64);

        printf("road = %d\r\n", cant0.busload);
    }
}

static void dev1_cant_task(void)
{
    cant_task(&cant1);
}

static void test_base(void)
{
    printf("cant test!\r\n");

    /* can bus init */
    canbus_init();
    task_create(5, canbus_task);

    /* can dev init */ 
    canbus_attach_dev(0, dev0_can_receive);
    canbus_attach_dev(1, dev1_can_receive);

    /* cant protcol init */
    cant_init(&cant0);
    cant_init(&cant1);
    task_create(5, dev0_cant_task);
    task_create(5, dev1_cant_task);

    cant_set_dummy_canid(&cant0, 0x777);
    cant_set_busload(&cant0, 3000);
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: cant [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;
    int period = 1000; // ms

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::p:");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'p' :
            period = atoi(command_optarg);
            break;
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("cant version %d.%d.%d\r\n", CANT_V_MAJOR, CANT_V_MINOR, CANT_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }
    
    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            #if defined(TEST_TARGET_cant)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_cant)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_cant(void)
{
    command_export("cant", test);
}
init_export_app(test_cant);
#endif 
